// pages/news-details/news-details.js
Page({
  data: {
    showModal: false,
    can_sign:'',
    service_id:'',
    open_sign:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    let service_id = options.service_id
    this.setData({
      service_id:options.service_id
    })
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/volunteerdetail',
      data:{
        access_token:access_token,
        service_id:service_id
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        } 
        else if(res.data.code == 1){
          this.setData({
            content:res.data.info.content,
            start_date:res.data.info.start_date,
            end_date:res.data.info.end_date,
            title:res.data.info.title,
            can_sign:res.data.info.can_sign,
            open_sign:res.data.info.open_sign
          })
        } 
      },
      complete:(res)=>{
        wx.hideLoading()
      }
    })
  },

  showModal() {
    if(this.data.can_sign == 1){
      this.setData({
        showModal: true
      })
    }else{
      wx.showToast({
        title: '该活动暂未开始或已经结束，无法报名',
        icon:'none',
        duration:2000
      })
    }
    
  },

  hideModal() {
    this.setData({
      showModal: false
    })
  },

  preventTouchMove() {
    // 弹框，阻止被遮住的内容滚动
  },
  
  // 报名
  formSubmit(e){
    let {realname,phone,remark} = e.detail.value
    let service_id = this.data.service_id
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/sign',
      method:'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        access_token:access_token,
        realname:realname,
        phone:phone,
        remark:remark,
        service_id:service_id
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          wx.showToast({
            title: '报名成功',
            success:(res)=>{
              setTimeout(()=>{
                wx.navigateBack()
              },1000)
            }
          })
        } 
      },
      complete:(res)=>{
  
      }
    })
  },

  cancel(){
    this.setData({
      showModal: false
    })
  }
})